package cn.ulyer.core.modules.system.rest;


import cn.hutool.core.lang.Assert;
import cn.ulyer.common.annotation.Log;
import cn.ulyer.common.constants.ResultCode;
import cn.ulyer.common.jpa.entity.Group;
import cn.ulyer.common.jpa.entity.GroupRole;
import cn.ulyer.common.jpa.entity.QGroup;
import cn.ulyer.common.jpa.entity.QGroupRole;
import cn.ulyer.common.model.PageableVo;
import cn.ulyer.common.utils.PageResult;
import cn.ulyer.common.utils.Result;
import cn.ulyer.core.modules.system.service.GroupRoleService;
import cn.ulyer.core.modules.system.service.TGroupService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * by generator - rest
 */
@RestController
@RequestMapping("/tGroupRest")
public class TGroupRest {

    final static String MODULE = "group";

    @Autowired
    private TGroupService tGroupService;


    @Autowired
    private GroupRoleService groupRoleService;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 分页
     */
    @GetMapping("/page")
    public Result<PageResult<Group>> page(PageableVo pageableVo, Group tGroup) {
        PageResult result = tGroupService.page(pageableVo.render(), tGroup);
        return Result.ok().data(result);
    }

    @GetMapping("/allGroup")
    public Result<List<Group>> allGroup(Group group) {
        QGroup qGroup = QGroup.group;
        JPAQuery<Group> typeQuery = jpaQueryFactory.selectFrom(qGroup);
        if (group != null && group.getStatus() != null) {
            typeQuery.where(qGroup.status.eq(group.getStatus()));
        }
        return Result.ok().data(typeQuery.fetch());
    }


    /**
     * key roles:[] groupId: integer
     *
     * @param jsonObject
     * @return
     */
    @PutMapping("/bindRoles")
    public Result bindRoles(@RequestBody JSONObject jsonObject) {
        Integer groupId = jsonObject.getInteger("groupId");
        Assert.notNull(groupId);
        JSONArray jsonArray = jsonObject.getJSONArray("roles");
        tGroupService.bindRoles(jsonArray.toJavaList(Integer.class), groupId);
        return Result.ok();
    }

    @GetMapping("/groupRoles")
    public Result<List<GroupRole>> groupRoles(@RequestParam Integer groupId) {
        QGroupRole qGroupRole = QGroupRole.groupRole;
        return Result.ok().data(groupRoleService.findAll(qGroupRole.groupId.eq(groupId)));
    }

    @GetMapping("/get")
    public Result<Group> detail(@RequestParam Integer id) {
        return Result.ok().data(tGroupService.getOne(id));
    }

    @PostMapping("/create")
    @Log(module = MODULE, action = Log.ActionName.CREATE)
    public Result<Integer> create(@RequestBody Group tGroup) {
        tGroupService.save(tGroup);
        return Result.ok(ResultCode.CREATE).data(tGroup.getId());
    }


    @PutMapping("/update")
    @Log(module = MODULE, action = Log.ActionName.UPDATE)
    public Result update(@RequestBody Group tGroup) {
        tGroupService.updateById(tGroup);
        return Result.ok(ResultCode.UPDATE);
    }

    /**
     * removeById
     */
    @DeleteMapping("/remove")
    @Log(module = MODULE, action = Log.ActionName.DELETE)
    public Result remove(@RequestParam Integer id) {
        tGroupService.deleteById(id);
        return Result.ok(ResultCode.DELETE);
    }


}
